﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace RenderTheBetterEllipse
{
    class BetterEllipse : FrameworkElement
    {
        public Brush Fill
        {
            get { return (Brush)GetValue(FillProperty); }
            set { SetValue(FillProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register("MyProperty", typeof(Brush), typeof(BetterEllipse), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));



        public Pen Stroke
        {
            get { return (Pen)GetValue(StrokeProperty); }
            set { SetValue(StrokeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Stroke.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StrokeProperty =
            DependencyProperty.Register("Stroke", typeof(Pen), typeof(BetterEllipse), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure));

        protected override Size MeasureOverride(Size availableSize)
        {
            Size sizeDesired = base.MeasureOverride(availableSize);
            if (this.Stroke != null)
            {
                sizeDesired = new Size(Stroke.Thickness, Stroke.Thickness);
            }

            return sizeDesired;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Size size = this.RenderSize;
            if (this.Stroke != null)
            {
                size.Width = Math.Max(0, size.Width - this.Stroke.Thickness);
                size.Height = Math.Max(0, size.Height - this.Stroke.Thickness);
            }

            //drawingContext.PushClip(new RectangleGeometry(new Rect(new Point(0, 0), this.RenderSize)));
            drawingContext.DrawEllipse(this.Fill, this.Stroke,
                new Point(this.RenderSize.Width / 2, this.RenderSize.Height / 2),
                size.Width / 2, size.Height / 2);

        }
    }
}
